<?php

declare(strict_types=1);

namespace App\Common\Modular\Administer\Crontab;

use App\Common\Modular\Administer\Model\LoginLogModel;
use App\Common\Modular\Administer\Serve\LoginLogServe;
use Core\HyperfUnit\Ip;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;

class LoginLogCrontab
{
    #[Inject]
    protected LoginLogServe $serve;

    /**
     * 设置登录信息IP属地
     */
    #[Crontab(rule: "*/30 * * * *", name: "loginip.ascription", memo: "解析用户登录IP属地,每30分钟执行一次")]
    public function setLoginAscription(): void
    {
        /**
         * @var LoginLogModel $item
         */
        $log = $this->serve->search("`ascription`=''")->limit(5)->get();
        foreach ($log as $item) {
            $item->ascription = Ip::ascription($item->ip);
            $item->save();
        }
    }
}
